package yt_api_test;

import com.google.gdata.client.*;
import com.google.gdata.client.youtube.*;
import com.google.gdata.data.*;
import com.google.gdata.data.geo.impl.*;
import com.google.gdata.data.media.*;
import com.google.gdata.data.media.mediarss.*;
import com.google.gdata.data.youtube.*;
import com.google.gdata.data.extensions.*;
import com.google.gdata.util.*;
import java.io.IOException;
import java.io.File;
import java.net.URL;

/**
 *
 * @author Kicok
 */
public class Test
{

    private static final String clientID = "YouTube Management Center";
    private static final String developersKey = "AI39si5vkI-xc6wsH6vV5ZuY3ez0OGfQ5C0UOEnwCKnQlzPWsVFzVBVS9VPvEKlsNe_JE7JN89u-fZ9_jU1MDztMtSt-DcOHEA";
    private static final String login = "morcin69";
    private static final String password = "morcin69";

    public static void main( String[] args )
    {
        try
        {
            YouTubeService service = new YouTubeService( clientID, developersKey );
            service.setUserCredentials( login, password );

            String userUploadedVideosFeedURL = "http://gdata.youtube.com/feeds/api/users/" + login + "/uploads";
            VideoFeed videoFeed = service.getFeed( new URL( userUploadedVideosFeedURL ), VideoFeed.class );
            printVideoFeed( videoFeed, true );
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
    }


    /**
     * @see http://code.google.com/intl/pl/apis/youtube/2.0/developers_guide_java.html#code_getAndPrintVideoFeed
     */
    private static void printVideoFeed( VideoFeed videoFeed, boolean detailed )
    {
        for( VideoEntry videoEntry : videoFeed.getEntries() )
        {
            printVideoEntry( videoEntry, detailed );
        }
    }

    /**
     * @see http://code.google.com/intl/pl/apis/youtube/2.0/developers_guide_java.html#Video_Entry_Contents
     */
    private static void printVideoEntry( VideoEntry videoEntry, boolean detailed )
    {
        System.out.println( "Title: " + videoEntry.getTitle().getPlainText() );

        if( videoEntry.isDraft() )
        {
            System.out.println( "Video is not live" );
            YtPublicationState pubState = videoEntry.getPublicationState();
            if( pubState.getState() == YtPublicationState.State.PROCESSING )
            {
                System.out.println( "Video is still being processed." );
            }
            else
            {
                if( pubState.getState() == YtPublicationState.State.REJECTED )
                {
                    System.out.print( "Video has been rejected because: " );
                    System.out.println( pubState.getDescription() );
                    System.out.print( "For help visit: " );
                    System.out.println( pubState.getHelpUrl() );
                }
                else
                {
                    if( pubState.getState() == YtPublicationState.State.FAILED )
                    {
                        System.out.print( "Video failed uploading because: " );
                        System.out.println( pubState.getDescription() );
                        System.out.print( "For help visit: " );
                        System.out.println( pubState.getHelpUrl() );
                    }
                }
            }
        }

        if( videoEntry.getEditLink() != null )
        {
            System.out.println( "Video is editable by current user." );
        }

        if( detailed )
        {
            YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup();

            System.out.println( "Uploaded by: " + mediaGroup.getUploader() );

            System.out.println( "Video ID: " + mediaGroup.getVideoId() );
            System.out.println( "Description: "
                + mediaGroup.getDescription().getPlainTextContent() );

            MediaPlayer mediaPlayer = mediaGroup.getPlayer();
            System.out.println( "Web Player URL: " + mediaPlayer.getUrl() );
            MediaKeywords keywords = mediaGroup.getKeywords();
            System.out.print( "Keywords: " );
            for( String keyword : keywords.getKeywords() )
            {
                System.out.print( keyword + "," );
            }

            GeoRssWhere location = videoEntry.getGeoCoordinates();
            if( location != null )
            {
                System.out.println( "Latitude: " + location.getLatitude() );
                System.out.println( "Longitude: " + location.getLongitude() );
            }

            Rating rating = videoEntry.getRating();
            if( rating != null )
            {
                System.out.println( "Average rating: " + rating.getAverage() );
            }

            YtStatistics stats = videoEntry.getStatistics();
            if( stats != null )
            {
                System.out.println( "View count: " + stats.getViewCount() );
            }
            System.out.println();

            System.out.println( "\tThumbnails:" );
            for( MediaThumbnail mediaThumbnail : mediaGroup.getThumbnails() )
            {
                System.out.println( "\t\tThumbnail URL: " + mediaThumbnail.getUrl() );
                System.out.println( "\t\tThumbnail Time Index: "
                    + mediaThumbnail.getTime() );
                System.out.println();
            }

            System.out.println( "\tMedia:" );
            for( YouTubeMediaContent mediaContent : mediaGroup.getYouTubeContents() )
            {
                System.out.println( "\t\tMedia Location: " + mediaContent.getUrl() );
                System.out.println( "\t\tMedia Type: " + mediaContent.getType() );
                System.out.println( "\t\tDuration: " + mediaContent.getDuration() );
                System.out.println();
            }

            for( YouTubeMediaRating mediaRating : mediaGroup.getYouTubeRatings() )
            {
                System.out.println( "Video restricted in the following countries: "
                    + mediaRating.getCountries().toString() );
            }
        }
    }
}
